package com.peach.SSMDemo.service;

/**
 * Created by tao on 2017/8/31.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.peach.SSMDemo.domain.FltRecord;
import com.peach.SSMDemo.domain.Header;
import java.util.List;

public class JSONService {

    public String praseJSON (List<FltRecord> fltRecords) throws JsonProcessingException {

        Header header = new Header();

        if (fltRecords.isEmpty()){

            header.setCode(0);
            header.setError("当天无航班");
            header.setMsg("航班列表为空，请选择别的日期");
            header.setStatus(0);
        }else {
            header.setCode(1);
            header.setError("");
            header.setMsg("航班查询成功");
            header.setStatus(1);
        }

        //头部

        Object objHeader = JSON.toJSON(header);



        //body部分：
        JSONObject jsonObj = new JSONObject();

        JSONArray jsonArr = new JSONArray();

        for (FltRecord fltRecord:
                fltRecords) {

            Object obj = JSON.toJSON(fltRecord);

            jsonArr.add(obj);
        }

        jsonObj.put("responsebody",jsonArr);

        jsonObj.put("header",objHeader);

        String body = JSON.toJSONString(jsonObj,true);

        return (body);


    }
}
